package com.github.lee.unsplash.ui.home

import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.MutableLiveData
import com.github.lee.mvvm.core.vm.BaseRefreshViewModel
import com.github.lee.unsplash.domain.PhotoBean
import com.github.lee.unsplash.model.repository.PhotoRepository
import com.github.unsplash.repository.repository.ParamOrientation
import com.github.unsplash.repository.repository.ParamPhotoOrderBy


class HomeVM @ViewModelInject constructor(private val rep: PhotoRepository) :
    BaseRefreshViewModel<PhotoBean, HomeAdapter>() {

    val topPhotoLiveData = MutableLiveData<PhotoBean?>()


    override fun onCreateAdapter(): HomeAdapter = HomeAdapter()


    fun homeIndex() {
        requestForResult({
            val topResult = rep.randomOnePhoto(orientation = ParamOrientation.LANDSCAPE)
            if (topResult.isSuccess()) {
                topPhotoLiveData.postValue(topResult.data)
            } else {
                topPhotoLiveData.postValue(null)
            }
            rep.listPhotos(1, 10, orderBy = ParamPhotoOrderBy.POPULAR)
        }, {
            if (!it.isSuccess()) {
                postError(msg = it.msg)
                return@requestForResult
            }
            val data = it.data
            if (data.isNullOrEmpty()) {
                postEmpty()
                return@requestForResult
            }
            setNewData(data)
            refreshLiveData.postValue(false)
            postSuccess()
        })
    }

    fun refreshOrLoad(isRefresh: Boolean) {
        if (isRefresh) {
            currentPage = 1
        } else {
            currentPage++
        }
        requestForResult({
            rep.listPhotos(currentPage, 10, orderBy = ParamPhotoOrderBy.POPULAR)
        }, {
            if (!it.isSuccess()) {
                postError(msg = it.msg)
                stopRefresh()
                return@requestForResult
            }
            val data = it.data
            if (data.isNullOrEmpty()) {
                if (isRefresh) {
                    postEmpty()
                } else {
                    loadMoreEnd()
                }
                return@requestForResult
            }
            if (isRefresh) {
                setNewData(data)
                stopRefresh()
            } else {
                addData(it.data)
                if (it.hasMore(currentPage)) {
                    loadMoreComplete()
                } else {
                    loadMoreEnd()
                }
            }
            postSuccess()
        })
    }

    fun likeOrUnlikePhoto(position: Int) {
        val item = contentAdapter.data[position]
        requestForResult({
            if (item.likedByUser) {
                rep.unlikePhoto(contentAdapter.data[position].id)
            } else {
                rep.likePhoto(contentAdapter.data[position].id)
            }
        }, {
            if (!it.isSuccess()) {
                postToast("${it.code}:${it.msg}")
                return@requestForResult
            }
            item.likedByUser = !item.likedByUser
            contentAdapter.setData(position, item)
        })
    }
}